#include "heating.h"
#include "air001_bsp.h"

extern ADC_HandleTypeDef AdcHandle;

// 初始化
void Heating_Init(Heating_Type *self, const uint16_t *temptab)
{
    self->power = 0;
    self->set_temp = 180;
    self->adjust_temp = 0;
    self->time = 0;
    self->state = 0;
    self->temp_list = temptab;
    self->power_max = 100; // 最大加热功率 0-100%  可以根据设置为温度进行调节
    // self->control_pid = &Temp_PID;      //共用一个PID控制器
    IncrementalPI_Init(self->control_pid, self->control_pid->Kp, self->control_pid->Ki); // 初始化PID控制器 没有对指针进行初始化
}

// 加热
void Heating_Start(Heating_Type *self)
{
    self->state = 1;
    self->time = 0;
    // printf("*Heating_Start\r\n");

}

// 停止加热
void Heating_Stop(Heating_Type *self)
{
    self->state = 0;
    self->power = 0;
    Change_Pulse_N(self->power * 5, 3);
    Change_Pulse_N(self->power * 5, 4);
    // printf("*Heating_Stop\r\n");

}

// 清除加热时间
void Heating_Clear_Time(Heating_Type *self)
{
    self->time = 0;
}

// 恒功率加热功率设置
void Heating_Set_Power(Heating_Type *self, uint8_t power)
{
    self->heating_power = power;
    self->heating_mode = 1;
    Change_Pulse_N(self->heating_power * 5, 3);
    Change_Pulse_N(self->heating_power * 5, 4);
}

// 加热温度设置
uint8_t Heating_Set_Temp(Heating_Type *self, uint8_t temp)
{
    if (temp > HEATING_TEMP_MAX)
    {
        temp = HEATING_TEMP_MAX;
        return 0;
    }
    self->heating_mode = 0;
    self->set_temp = temp;
    // printf("*Heating.set_temp = %d\r\n",self->set_temp);

    return 1;
}

// 加热时间设置
void Heating_Set_Time(Heating_Type *self, uint16_t time)
{
    self->time = time;
}

// 加热时间获取
uint16_t Heating_Get_Time(Heating_Type *self)
{
    return self->time;
}

// 加热状态获取
uint8_t Heating_Get_State(Heating_Type *self)
{
    return self->state;
}

// 累计加热时间获取
uint16_t Heating_Get_Work_Time(Heating_Type *self)
{
    return self->work_time;
}

// 设置调整温度
void Heating_set_adjust_temp(Heating_Type *self, signed char temp)
{
    self->adjust_temp = temp;
}

// 设置最大输出功率
void Heating_set_power_max(Heating_Type *self, uint8_t power)
{
    self->power_max = power;
}
// 获取调整温度
signed char Heating_get_adjust_temp(Heating_Type *self)
{
    return self->adjust_temp;
}

// AD值转换为温度
static void Heating_get_Template(Heating_Type *self)
{
    uint8_t temp = 0;
    while (1)
    {
        if (self->temp_list[temp] < adc_value[1])
        {
            if (temp < HEATING_TEMP_MAX)
            {
                temp++;
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
    self->actual_temp = temp + self->adjust_temp; // 获取实时温度
}

// 加热处理
void Heating_deal_100ms(Heating_Type *self)
{
    static uint8_t i = 0;
    if (i++ > 10)
    {
        i = 0;
        if (self->state == 1)
        {
            self->work_time++;
            self->time++;
        }
    }

    Heating_get_Template(self);                                              // 获取实时温度
    self->set_temp_AD = self->temp_list[self->set_temp + self->adjust_temp]; // 获取实时温度
    self->actual_temp_AD = adc_value[1];                                     // 获取实时温度
#if DEBUG_PRINTF_HELP
    printf("温度 = %d\r\n", self->actual_temp);
#endif

    // 增加PID控制
    if (self->state > 0) // 加热中
    {
        if (self->heating_mode == 0)
        {
            // printf("*control_pid = %d %d %d \r\n",self->control_pid->Kp,self->control_pid->Ki,self->control_pid->Kd);
            // printf("*set_temp_AD = %d\r\n",self->set_temp_AD);
            // printf("*actual_temp_AD = %d\r\n",self->actual_temp_AD);

            // 恒温加热
            self->power = IncrementalPI_Compute(self->control_pid, self->set_temp_AD, self->actual_temp_AD, self->power_max);
#if DEBUG_PRINTF_HELP
            printf("恒温加热\r\n");
            printf("power = %d , temp = %d \r\n", self->power, self->actual_temp);
#endif
        }
        else
        {
            // 功率加热
            self->power = self->heating_power;
#if DEBUG_PRINTF_HELP
            printf("恒功率加热\r\n");
            printf("power = %d , temp = %d \r\n", self->power, self->actual_temp);
#endif
        }

        Change_Pulse_N(self->power * 5, 3);
        Change_Pulse_N(self->power * 5, 4);
    }
    else
    {
        Change_Pulse_N(0, 3);
        Change_Pulse_N(0, 4);
    }
}
